import { createApi } from 'kuan-request'
import { notification } from 'ant-design-vue'
import authority from './authority'
import router from '@/router'

const request = createApi({
  toast: message => notification.open({ message }), // 自定义提示
  setHeaders: () => {
    const { token } = authority.get() || {}
    const headers = {
      'X-Halo-App': 'wfc2018-dashboard'
    }
    if (token) {
      headers.Authorization = `Bearer ${token}`
    }
    return headers
  },
  handleError: (status, msg) => {
    console.log(`${status ? status + ':' : ''}${msg}`)
    authority.clear()
    if (status === 401) router.push({ path: '/login' })
  }
})

export default request
